import { GButton } from "./GButton";
import { Alert } from "./Alert";
import { Button } from "../../../fl/controls/Button";
import { Vector } from "../../../__AS3__/vec/Vector";
type int = number;
//class GAlertWin
    
    export  class GAlertWin /* flash.display.Sprite */
    {
        constructor(arg1: number, arg2: number)
        {
            
            this.allButtons = new Vector.<GButton>();
            this.stageWidth = arg1;
            this.stageHeight = arg2;
            this.createChildren();
            this.childrenCreated();
            this.addDragHandleer();
            this.closeHandler = Alert.closeHandler;
            this.extendObj = Alert.extendObj;
            this.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }

        protected createBackground(): Object /* flash.display.DisplayObject */
        {
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=0;
            var loc8=null;
            var loc1=new Object /* flash.display.Sprite */();
            loc2 = Alert.backgroundColor;
            var loc9=Alert.mode;
            switch (loc9) 
            {
                case Alert.Mode_Blur:
                {
                    loc3 = new Object /* flash.display.BitmapData */(this.stageWidth, this.stageHeight, true, 4278190080 + loc2);
                    (loc4 = new Object /* flash.display.BitmapData */(this.stageWidth, this.stageHeight)).draw(stage);
                    loc5 = new Object /* flash.geom.Rectangle */(0, 0, this.stageWidth, this.stageHeight);
                    loc6 = new Object /* flash.geom.Point */(0, 0);
                    loc7 = 120;
                    loc3.merge(loc4, loc5, loc6, loc7, loc7, loc7, loc7);
                    loc3.applyFilter(loc3, loc5, loc6, new Object /* flash.filters.BlurFilter */(5, 5));
                    loc8 = new Object /* flash.display.Bitmap */(loc3);
                    loc1.addChild(loc8);
                    loc1.mouseEnabled = true;
                    break;
                }
                case Alert.Mode_None:
                {
                    loc1.graphics.beginFill(loc2, 0);
                    loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
                    loc1.graphics.endFill();
                    loc1.mouseEnabled = true;
                    break;
                }
                case Alert.Mode_NoneNotModal:
                {
                    loc1.graphics.beginFill(loc2, 0);
                    loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
                    loc1.graphics.endFill();
                    loc1.mouseEnabled = false;
                    break;
                }
                case Alert.Mode_Simple:
                {
                    loc1.graphics.beginFill(loc2, Alert.backgroundAlpha);
                    loc1.graphics.drawRect(0, 0, this.stageWidth, this.stageHeight);
                    loc1.graphics.endFill();
                    loc1.mouseEnabled = true;
                    break;
                }
            }
            return loc1;
        }

        protected doStopDrag(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target is flash.display.Sprite) 
            {
                arg1.currentTarget.stopDrag();
            }
            return;
        }

        protected getBlurFilter(): Object /* flash.filters.BitmapFilter */
        {
            var loc1=100;
            var loc2=100;
            return new Object /* flash.filters.BlurFilter */(loc1, loc2, flash.filters.BitmapFilterQuality.HIGH);
        }

        protected getDropShadowFilter(): Object /* flash.filters.DropShadowFilter */
        {
            var loc1=0;
            var loc2=90;
            var loc3=0.6;
            var loc4=12;
            var loc5=4;
            var loc6=1;
            var loc7=1;
            var loc8=false;
            var loc9=false;
            var loc10=flash.filters.BitmapFilterQuality.LOW;
            return new Object /* flash.filters.DropShadowFilter */(loc6, loc2, loc1, loc3, loc4, loc5, loc7, loc10, loc8, loc9);
        }

        protected addDragHandleer(): void
        {
            this.prompt.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, GAlertWin.doStartDrag);
            this.prompt.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.doStopDrag);
            return;
        }

        protected getGlowFilter(): Object /* flash.filters.GlowFilter */
        {
            var loc1=16777215;
            var loc2=0.8;
            var loc3=15;
            var loc4=15;
            var loc5=0.7;
            var loc6=true;
            var loc7=false;
            var loc8=flash.filters.BitmapFilterQuality.HIGH;
            return new Object /* flash.filters.GlowFilter */(loc1, loc2, loc3, loc4, loc5, loc8, loc6, loc7);
        }

        protected RGBToHex(arg1: int, arg2: int, arg3: int): int
        {
            var loc1=0;
            return loc1 = (loc1 = (loc1 = arg1 & 255 << 16) + (arg2 & 255 << 8)) + (arg3 & 255);
        }

        protected keepInBounds(arg1: int): int
        {
            if (arg1 < 0) 
            {
                arg1 = 0;
            }
            if (arg1 > 255) 
            {
                arg1 = 255;
            }
            return arg1;
        }

        protected buttonClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            while (loc1 < this.allButtons.length) 
            {
                this.allButtons[loc1].removeEventListener(flash.events.MouseEvent.CLICK, this.buttonClickHandler);
                ++loc1;
            }
            this.removeDragHandleer();
            this.parent.removeChild(this);
            if (this.closeHandler != null) 
            {
                if (this.extendObj == null) 
                {
                    this.closeHandler(Number(arg1.target.name.substr(4)));
                }
                else 
                {
                    this.closeHandler(Number(arg1.target.name.substr(4)), this.extendObj);
                }
            }
            return;
        }

        protected onResize(arg1: Object /* flash.events.Event */): void
        {
            if (this.background) 
            {
                if (this.stage) 
                {
                    this.background.width = this.stage.stageWidth;
                    this.background.height = this.stage.stageHeight;
                }
            }
            return;
        }

        public get promptBackground(): Object /* flash.display.DisplayObject */
        {
            return this._promptBackground;
        }

        public getButtonByFlag(arg1: int): GButton
        {
            var loc1=null;
            var loc2=0;
            var loc3=this.allButtons;
            for(loc1 of loc3) 
            {
                if (loc1.name != "btn_" + arg1) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        protected static doStartDrag(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target is flash.display.Sprite && !(arg1.target is Button)) 
            {
                arg1.currentTarget.startDrag();
            }
            return;
        }

        protected static ByteToArray(arg1: uint): Array<any>
        {
            var loc1=new Array<any>();
            loc1.push(arg1 & 1);
            loc1.push((arg1 & 2) >> 1);
            loc1.push((arg1 & 4) >> 2);
            loc1.push((arg1 & 8) >> 3);
            return loc1;
        }

        protected static hexToRGB(arg1: uint): Array<any>
        {
            var loc1=new Array<any>();
            loc1.push(arg1 >> 16);
            var loc2=arg1 ^ loc1[0] << 16;
            loc1.push(loc2 >> 8);
            loc1.push(loc2 ^ loc1[1] << 8);
            return loc1;
        }

        /* internal  */onAddToStage(arg1: Object /* flash.events.Event */): void
        {
            this.stage.addEventListener(flash.events.Event.RESIZE, this.onResize);
            return;
        }

        /* internal  */onRemoveFromStage(arg1: Object /* flash.events.Event */): void
        {
            this.stage.removeEventListener(flash.events.Event.RESIZE, this.onResize);
            return;
        }

        protected onCloseBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=0;
            while (loc1 < this.allButtons.length) 
            {
                this.allButtons[loc1].removeEventListener(flash.events.MouseEvent.CLICK, this.buttonClickHandler);
                ++loc1;
            }
            if (this.closeBtn != null) 
            {
                this.closeBtn.removeEventListener(flash.events.MouseEvent.CLICK, this.onCloseBtnClickHandler);
            }
            if (this.closeHandler != null) 
            {
                if (this.extendObj == null) 
                {
                    this.closeHandler(Alert.CLOSE);
                }
                else 
                {
                    this.closeHandler(Alert.CLOSE, this.extendObj);
                }
            }
            this.removeDragHandleer();
            this.parent.removeChild(this);
            return;
        }

        protected createChildren(): void
        {
            if (Alert.title is flash.display.DisplayObject) 
            {
                this.title = Alert.title;
            }
            else 
            {
                this.title = this.createTextField();
                if (Alert.titleTextFormat) 
                {
                    (this.title as Object /* flash.text.TextField */).defaultTextFormat = Alert.titleTextFormat;
                }
            }
            this.message = this.createTextField();
            if (!Alert.messageTextFormat) 
            {
            };
            this.background = this.createBackground();
            this.boarder = this.createPromptBoarder();
            this.allButtons = this.createButtons();
            if (Alert.iconClass) 
            {
                this.icon = new Alert.iconClass();
            }
            this.showCloseBtn();
            return;
        }

        protected showCloseBtn(): void
        {
            if (Alert.showCloseBtn) 
            {
                this.closeBtn = new GButton();
                this.closeBtn.styleName = "CloseButton";
                this.closeBtn.setSize(19, 19);
                this.closeBtn.label = "";
                this.closeBtn.configEventListener(flash.events.MouseEvent.CLICK, this.onCloseBtnClickHandler);
                this._promptBackground.addChild(this.closeBtn);
            }
            return;
        }

        protected childrenCreated(): void
        {
            var loc8=null;
            var loc1=this.title as Object /* flash.text.TextField */;
            loc1.text = Alert.title;
            this.message.text = Alert.text;
            var loc2=new Object /* flash.display.Sprite */();
            var loc3=0;
            while (loc3 < this.allButtons.length) 
            {
                (loc8 = this.allButtons[loc3]).addEventListener(flash.events.MouseEvent.CLICK, this.buttonClickHandler);
                loc2.addChild(loc8);
                loc8.x = loc3 * (Alert.buttonWidth + 15);
                ++loc3;
            }
            var loc4;
            (loc4 = new Object /* flash.display.Sprite */()).addChild(this.message);
            if (this.icon) 
            {
                loc4.addChild(this.icon);
                this.message.x = this.icon.width + 15;
            }
            var loc5=this.boarderWidth * 2;
            var loc6=Math.max(this.title.width + loc5, this.message.width + loc5, loc2.width + loc5, this.minWidth);
            var loc7=Math.max(this.title.height + 5, 15) + this.message.height + Alert.buttonHeight + 45;
            loc1.autoSize = flash.text.TextFieldAutoSize.NONE;
            loc1.width = loc6 - 10;
            loc1.x = 5;
            loc1.y = 5;
            this.message.x = loc6 / 2 - this.message.width / 2;
            this.message.y = Math.max(this.title.height + 15, 15);
            loc2.x = (loc6 - (Alert.buttonWidth + 15) * this.allButtons.length + 15) / 2;
            loc2.y = this.message.y + this.message.height + 15;
            this.boarder.width = loc6;
            this.boarder.height = loc7;
            this.prompt = new Object /* flash.display.Sprite */();
            addChild(this.background);
            this.prompt.addChild(this.boarder);
            this.prompt.addChild(this.title);
            this.prompt.addChild(this.message);
            if (this.icon) 
            {
                addChild(this.icon);
            }
            this.prompt.addChild(loc2);
            addChild(this.prompt);
            this.prompt.x = (this.stageWidth - this.prompt.width) / 2;
            this.prompt.y = (this.stageHeight - this.prompt.height) / 2;
            if (Alert.showCloseBtn) 
            {
                this.closeBtn.x = loc6 - this.closeBtn.width - 8;
                this.closeBtn.y = 7;
            }
            return;
        }

        public get window(): Object /* flash.display.DisplayObjectContainer */
        {
            return this.prompt;
        }

        protected removeDragHandleer(): void
        {
            this.prompt.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, GAlertWin.doStartDrag);
            this.prompt.removeEventListener(flash.events.MouseEvent.MOUSE_UP, this.doStopDrag);
            return;
        }

        protected createPromptBoarder(): Object /* flash.display.DisplayObject */
        {
            this._promptBackground = new Object /* flash.display.Sprite */();
            var loc1=10;
            this._promptBackground.graphics.lineStyle(1);
            this._promptBackground.graphics.beginFill(Alert.backgroundColor);
            this._promptBackground.graphics.drawRoundRect(0, 0, 100, 100, loc1, loc1);
            this._promptBackground.graphics.endFill();
            this._promptBackground.scale9Grid = new Object /* flash.geom.Rectangle */(20, 20, 60, 60);
            this._promptBackground.filters = [this.getGlowFilter(), this.getDropShadowFilter()];
            this._promptBackground.alpha = Alert.backgroundAlpha;
            return this._promptBackground;
        }

        protected createButtons(): Vector.<GButton>
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc1=new Vector.<GButton>();
            var loc2=this.getButtonFlagsList();
            if (loc2[0] == 1) 
            {
                loc3 = new GButton();
                loc3.label = Alert.yesLabel;
                loc3.name = "btn_" + Alert.YES;
                loc3.setSize(Alert.buttonWidth, Alert.buttonHeight);
                if (Alert.buttonStyleName) 
                {
                    loc3.styleName = Alert.buttonStyleName;
                }
                loc1.push(loc3);
            }
            if (loc2[1] == 1) 
            {
                (loc4 = new GButton()).label = Alert.noLabel;
                loc4.name = "btn_" + Alert.NO;
                loc4.setSize(Alert.buttonWidth, Alert.buttonHeight);
                if (Alert.buttonStyleName) 
                {
                    loc4.styleName = Alert.buttonStyleName;
                }
                loc1.push(loc4);
            }
            if (loc2[2] == 1) 
            {
                (loc5 = new GButton()).label = Alert.okLabel;
                loc5.name = "btn_" + Alert.OK;
                loc5.setSize(Alert.buttonWidth, Alert.buttonHeight);
                if (Alert.buttonStyleName) 
                {
                    loc5.styleName = Alert.buttonStyleName;
                }
                loc1.push(loc5);
            }
            if (loc2[3] == 1) 
            {
                (loc6 = new GButton()).label = Alert.calcelLabel;
                loc6.name = "btn_" + Alert.CANCEL;
                loc6.setSize(Alert.buttonWidth, Alert.buttonHeight);
                if (Alert.buttonStyleName) 
                {
                    loc6.styleName = Alert.buttonStyleName;
                }
                loc1.push(loc6);
            }
            return loc1;
        }

        protected getButtonFlagsList(): Array<any>
        {
            var loc1=null;
            if (Alert.buttonFlags == 0) 
            {
                loc1 = GAlertWin.ByteToArray(Alert.defaultButtonFlag);
            }
            else 
            {
                loc1 = GAlertWin.ByteToArray(Alert.buttonFlags);
            }
            return loc1;
        }

        protected createTextField(): Object /* flash.text.TextField */
        {
            var loc1=new Object /* flash.text.TextField */();
            loc1.textColor = Alert.textColor;
            loc1.multiline = true;
            loc1.selectable = false;
            loc1.mouseEnabled = false;
            loc1.autoSize = flash.text.TextFieldAutoSize.LEFT;
            return loc1;
        }

        protected /* var */title: Object /* flash.display.DisplayObject */;

        protected /* var */message: Object /* flash.text.TextField */;

        protected /* var */background: Object /* flash.display.DisplayObject */;

        protected /* var */prompt: Object /* flash.display.DisplayObjectContainer */;

        protected /* var */boarder: Object /* flash.display.DisplayObject */;

        protected /* var */icon: Object /* flash.display.DisplayObject */;

        protected /* var */boarderWidth: int=15;

        protected /* var */minWidth: int=150;

        protected /* var */minHeight: int=150;

        protected /* var */stageWidth: number;

        protected /* var */stageHeight: number;

        protected /* var */closeHandler: Function;

        protected /* var */closeBtn: GButton;

        protected /* var */extendObj: Object;

        protected /* var */allButtons: Vector.<GButton>;

        protected /* var */_promptBackground: Object /* flash.display.Sprite */;
    }
